﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Project1
{
    public class Player : MovableObject
    {
        const string ASSET_NAME = "boy";
        bool _onGround = false;
        KeyboardState oldstate;

        public Player(Game1 g, Vector2 position)
            : base(g, ASSET_NAME, position)
        {
            oldstate = Keyboard.GetState();
            Acceleration = new Vector2(0.0f, -600.0f);
        }

        // Whether the player is currently standing on a block
        public bool OnGround { 
            get
            {
                return _onGround;
            }
            set 
            {
                if (value)
                {
                    Velocity = new Vector2(0, 0);
                }
                _onGround = value;
            } 
        }

        public override void Update(GameTime gameTime)
        {
            var _game = Game as Game1;
            var newstate = Keyboard.GetState();
            float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;

            //how much movement in the x direction from left and right keys
            const float xscaling = 100;
            //how fast movement in the y direction is when jumping or falling
            const float yscaling = 310;

            if (newstate.IsKeyDown(Keys.Left))
            {
                Position -= new Vector2(xscaling * elapsedSeconds, 0);
            }
            if (newstate.IsKeyDown(Keys.Right))
            {
                Position += new Vector2(xscaling * elapsedSeconds, 0);
            }
            if (newstate.IsKeyDown(Keys.Up))
            {
                if (OnGround)
                {
                    Velocity = new Vector2(0, 1.0f * yscaling);
                }
                _onGround = false;
            }

            if (Position.Y <= 0)
                Position = _game.PlayerStart;//new Vector2(100, 270);

            oldstate = newstate;
            base.Update(gameTime);
        }
    }
}
